home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / play / tracker_4_31.lzh / tracker / resample.c < prev    next >
C/C++ Source or Header  |  1995-03-10  |  11KB  |  421 lines

  1. /* resample.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: resample.c,v 4.22 1995/03/01 15:24:51 espie Exp espie $
  6.  * $Log: resample.c,v $
  7.  * Revision 4.22  1995/03/01  15:24:51  espie
  8.  * - implemented data width notion.
  9.  * - set what happened is set_position overflowed the sample
  10.  *  -> change behavior to DO_NOTHING from go on repeating.
  11.  *
  12.  * Revision 4.21  1995/02/27  14:24:23  espie
  13.  * Minor bug in dump_delimiter.
  14.  *
  15.  * Revision 4.20  1995/02/23  23:33:01  espie
  16.  * Linear resampling changed.
  17.  *
  18.  * Revision 4.19  1995/02/23  22:40:44  espie
  19.  * Changed call to output_samples, added # of bits.
  20.  *
  21.  * Revision 4.18  1995/02/23  13:49:41  espie
  22.  * Checked optimization -> all the time for oversample == 1 is spent
  23.  * for dividing by oversample in the main loop. Recoded accordingly.
  24.  * Suppressed special case. With the division in leff/right channels
  25.  * from the start, recognized that the inner loop sampling=0..oversample
  26.  * was no longer necessary... just output summed sample once every
  27.  * oversample sample.
  28.  *
  29.  * Revision 4.17  1995/02/21  21:13:16  espie
  30.  * Cleaned up source. Moved minor pieces of code around.
  31.  *
  32.  * Revision 4.16  1995/02/21  17:54:32  espie
  33.  * Internal problem: buggy RCS. Fixed logs.
  34.  *
  35.  * Revision 4.11  1995/02/06  14:50:47  espie
  36.  * Changed sample_info.
  37.  *
  38.  * Revision 4.10  1995/02/01  17:14:54  espie
  39.  * Scaled volume.
  40.  *
  41.  * Revision 4.9  1995/02/01  16:39:04  espie
  42.  * Implemented Left/Right channels, 23 bits output.
  43.  *
  44.  * Revision 4.4  1994/08/23  18:19:46  espie
  45.  * New sampling function.
  46.  * Finally corrected irksome probleme with MAX_PITCH.
  47.  * First changes to augment the number of channels.
  48.  * Added one level of abstraction. Look through player.c for the
  49.  * old high-level functions.
  50.  * Optimized output and fixed up some details.
  51.  * Unrolled code for oversample = 1 to be more efficient at
  52.  * higher frequency (since a high frequency is better than
  53.  * a higher oversample).
  54.  *
  55.  * Revision 2.14  1992/11/06  19:31:53  espie
  56.  * Fixed missing parameter type.
  57.  * fix_xxx for better speed.
  58.  * set_volume.
  59.  * Added possibility to get back to MONO for the sgi.
  60.  * Added stereo capabilities to the indigo version.
  61.  * Minor bug: a SAMPLE_FAULT is a minor error,
  62.  * we should first check that there was no other
  63.  * error before setting it.
  64.  * New resample function coming from the player.
  65.  * Added more notes.
  66.  *
  67.  * Revision 2.1  1991/11/17  23:07:58  espie
  68.  * Just computes some frequency-related parameters.
  69.  *
  70.  *
  71.  */
  72.  
  73. #include <math.h>
  74.  
  75. #include "defs.h"
  76. #include "song.h"
  77. #include "channel.h"
  78. #include "tags.h"
  79. #include "extern.h"
  80.      
  81. ID("$Id: resample.c,v 4.22 1995/03/01 15:24:51 espie Exp espie $")
  82.  
  83. /* DO_NOTHING is also used for the automaton */
  84. #define DO_NOTHING 0
  85. #define PLAY 1
  86. #define REPLAY 2
  87.  
  88.  
  89. #define MAX_CHANNELS 8
  90.  
  91. LOCAL struct audio_channel
  92.    {
  93.    struct sample_info *samp;
  94.    int mode;
  95.    unsigned long pointer;
  96.    unsigned long step;
  97.    int volume;
  98.     int scaled_volume;
  99.    int pitch;
  100.     int side;
  101.    } chan[MAX_CHANNELS];
  102.  
  103. /* left/right */
  104. #define NUMBER_SIDES 2
  105. LOCAL int total[NUMBER_SIDES];
  106. LOCAL int allocated = 0;
  107.  
  108. struct audio_channel *new_channel_tag_list(prop)
  109. struct tag *prop;
  110.    {
  111.    struct audio_channel *new;
  112.  
  113.    new = &chan[allocated++];
  114.    new->mode = DO_NOTHING;
  115.    new->pointer = 0;
  116.    new->step = 0;
  117.    new->pitch = 0;
  118.    new->volume = 0;
  119.     new->scaled_volume = 0;
  120.    new->samp = empty_sample();
  121.  
  122.     while (prop = get_tag(prop))
  123.         {
  124.         switch(prop->type)
  125.             {
  126.         case AUDIO_SIDE:
  127.             new->side = prop->data.scalar;
  128.             break;
  129.         default:
  130.             break;
  131.             }
  132.         prop++;
  133.         }
  134.     total[new->side]++;
  135.    return new;
  136.    }
  137.  
  138. void release_audio_channels()
  139.    {
  140.    allocated = 0;
  141.    }
  142.  
  143. /* Have to get some leeway for vibrato (since we don't bound pitch with
  144.  * vibrato). This is conservative.
  145.  */
  146. #define VIB_MAXDEPTH 150
  147.  
  148.  
  149. #define C fix_to_int(ch->pointer)
  150.  
  151. LOCAL unsigned long step_table[REAL_MAX_PITCH + VIB_MAXDEPTH];  
  152.                   /* holds the increment for finding the next sampled
  153.                    * byte at a given pitch (see resample() ).
  154.                    */
  155.  
  156. /* create a table for converting ``amiga'' pitch
  157.  * to a step rate at a given resampling frequency.
  158.  * For accuracy, we don't use floating point, but
  159.  * instead fixed point ( << ACCURACY).
  160.  * IMPORTANT NOTES:
  161.  * - we need to make it fit within 32 bits (long), which must be enough 
  162.  * for ACCURACY + log2(max sample length)
  163.  * - for linear resampling to work correctly, we need 
  164.  * sample size (8 bit) + volume size (6 bit) + ACCURACY to fit within a
  165.  * long. Appropriate steps will have to be taken when we switch to 16 bit
  166.  * samples...
  167.  * - never forget that samples are SIGNED numbers. If you have unsigned 
  168.  * samples, you have to convert them SOMEWHERE.
  169.  */
  170. LOCAL void create_step_table(oversample, output_fr)
  171. int oversample;     /* we sample oversample i for each byte output */
  172. int output_fr;      /* output frequency */
  173.    {
  174.    double note_fr; /* note frequency (in Hz) */
  175.    double step;
  176.    int pitch;      /* amiga pitch */
  177.  
  178.         /* special case: oversample of 0 means linear resampling */
  179.     if (oversample == 0)
  180.         oversample = 1;
  181.    step_table[0] = 0;
  182.    for (pitch = 1; pitch < REAL_MAX_PITCH + VIB_MAXDEPTH; pitch++)
  183.       {
  184.       note_fr = AMIGA_CLOCKFREQ / pitch;
  185.          /* int_to_fix(1) is the normalizing factor */
  186.       step = note_fr / output_fr * int_to_fix(1) / oversample;
  187.       step_table[pitch] = (long)step;
  188.       }
  189.    }
  190.          
  191. LOCAL void readjust_pitch()
  192.    {
  193.    int i;
  194.    for (i = 0; i < allocated; i++)
  195.       chan[i].step = step_table[chan[i].pitch];
  196.    }
  197.  
  198. void init_tables(oversample, frequency)
  199. int oversample, frequency;
  200.    {
  201.    create_step_table(oversample, frequency);
  202.    readjust_pitch();
  203.    }
  204.  
  205.  
  206. LOCAL int max_side;        /* number of bits on one side */
  207.  
  208. LOCAL int max_sample;    /* number of bits for one sample */
  209.  
  210. void set_data_width(side, sample)
  211. int side, sample;
  212.     {
  213.     max_side = side;
  214.     max_sample = sample;
  215.     }
  216.  
  217. /* The playing mechanism itself.
  218.  * According to the current channel automaton,
  219.  * we resample the instruments in real time to
  220.  * generate output.
  221.  */
  222. void resample(oversample, number)
  223. int oversample;
  224. int number;
  225.    {
  226.    int i;            /* sample counter */
  227.    int channel;      /* channel counter */
  228.    int sampling;     /* oversample counter */
  229.     int step;         /* fractional part for linear resampling */
  230.    long value[NUMBER_SIDES];
  231.                      /* recombinations of the various data */
  232.    struct audio_channel *ch;
  233.  
  234.       /* safety check: we can't have a segv there, provided
  235.        * chan points to a valid sample.
  236.        * For `empty' samples, what is needed is fix_length = 0
  237.        * and rp_start = NULL
  238.        */
  239.  
  240.         /* do the resampling, i.e., actually play sounds */
  241.         /* code unfolding for special cases */
  242.     switch(oversample)
  243.         {
  244.     case 0:    /* linear resampling */
  245.       for (i = 0; i < number; i++) 
  246.          {
  247.             value[LEFT_SIDE] = value[RIGHT_SIDE] = 0;
  248.          for (channel = 0; channel < allocated; channel++)
  249.             {
  250.             ch = chan + channel;
  251.             switch(ch->mode)
  252.                {
  253.             case DO_NOTHING:
  254.                break;
  255.             case PLAY:
  256.                   /* Since we now have fix_length, we can
  257.                    * do that check with improved performance
  258.                    */
  259.                if (ch->pointer < ch->samp->fix_length)
  260.                   {
  261.                         step = fractional_part(ch->pointer);
  262.                         value[ch->side] += 
  263.                              (ch->samp->start[C] * (total_step - step) +
  264.                                ch->samp->start[C+1] * step)
  265.                              * (ch->scaled_volume);
  266.                   ch->pointer += ch->step;
  267.                   break;
  268.                   }
  269.                else
  270.                   {
  271.                   ch->mode = REPLAY;
  272.                   ch->pointer -= ch->samp->fix_length;
  273.                   /* FALLTHRU */
  274.                   }
  275.             case REPLAY:
  276.                      /* is there a replay ? */
  277.                if (!ch->samp->rp_start)
  278.                   {
  279.                   ch->mode = DO_NOTHING;
  280.                   break;
  281.                   }
  282.                while (ch->pointer >= ch->samp->fix_rp_length)
  283.                   ch->pointer -= ch->samp->fix_rp_length;
  284.                     step = fractional_part(ch->pointer);
  285.                     value[ch->side] += 
  286.                          (ch->samp->rp_start[C] * (total_step - step) +
  287.                             ch->samp->rp_start[C+1] * step)
  288.                          * ch->scaled_volume ;
  289.                ch->pointer += ch->step;
  290.                break;
  291.                }
  292.             } 
  293.                 /* some assembly required... */
  294.          output_samples(value[LEFT_SIDE], value[RIGHT_SIDE], ACCURACY+max_side);
  295.          }
  296.         break;
  297.     default:        /* standard oversampling code */
  298.         value[LEFT_SIDE] = value[RIGHT_SIDE] = 0;
  299.         i = sampling = 0;
  300.         while(TRUE)
  301.          {
  302.          for (channel = 0; channel < allocated; channel++)
  303.                 {
  304.                 ch = chan + channel;
  305.                 switch(ch->mode)
  306.                     {
  307.                 case DO_NOTHING:
  308.                     break;
  309.                 case PLAY:
  310.                         /* Since we now have fix_length, we can
  311.                          * do that check with improved performance
  312.                          */
  313.                     if (ch->pointer < ch->samp->fix_length)
  314.                         {
  315.                         value[ch->side] += ch->samp->start[C] * ch->scaled_volume;
  316.                         ch->pointer += ch->step;
  317.                         break;
  318.                         }
  319.                     else
  320.                         {
  321.                         ch->mode = REPLAY;
  322.                         ch->pointer -= ch->samp->fix_length;
  323.                         /* FALLTHRU */
  324.                         }
  325.                 case REPLAY:
  326.                             /* is there a replay ? */
  327.                     if (!ch->samp->rp_start)
  328.                         {
  329.                         ch->mode = DO_NOTHING;
  330.                         break;
  331.                         }
  332.                     while (ch->pointer >= ch->samp->fix_rp_length)
  333.                         ch->pointer -= ch->samp->fix_rp_length;
  334.                     value[ch->side] += ch->samp->rp_start[C] * ch->scaled_volume;
  335.                     ch->pointer += ch->step;
  336.                     break;
  337.                     }
  338.                 }
  339.             if (++sampling >= oversample)
  340.                 {
  341.                 sampling = 0;
  342.                 switch(oversample)
  343.                     {
  344.                 case 1:
  345.                     output_samples(value[LEFT_SIDE],
  346.                         value[RIGHT_SIDE], max_side);
  347.                     break;
  348.                 case 2:
  349.                     output_samples(value[LEFT_SIDE], 
  350.                         value[RIGHT_SIDE], max_side+1);
  351.                     break;
  352.                 case 4:
  353.                     output_samples(value[LEFT_SIDE],
  354.                         value[RIGHT_SIDE], max_side+2);
  355.                     break;
  356.                 default:
  357.                     output_samples(value[LEFT_SIDE]/oversample,
  358.                         value[RIGHT_SIDE]/oversample, max_side);
  359.                     }
  360.                 
  361.                 value[LEFT_SIDE] = value[RIGHT_SIDE] = 0;
  362.                 if (++i >= number) 
  363.                     break;
  364.                 }
  365.          }   
  366.       }
  367.  
  368.    flush_buffer();
  369.    }
  370.  
  371.  
  372. /* setting up a given note */
  373. void play_note(au, samp, pitch)
  374. struct audio_channel *au;
  375. struct sample_info *samp;
  376. int pitch;
  377.    {
  378.    au->pointer = 0;
  379.    au->pitch = pitch;
  380.    au->step = step_table[pitch];
  381.     au->samp = samp;
  382.     au->scaled_volume = au->samp->volume_lookup[au->volume];
  383.     au->mode = PLAY;
  384.    }
  385.  
  386. /* changing the current pitch (value may be temporary, and not stored
  387.  * in channel pitch, for instance for vibratos)
  388.  */
  389. void set_play_pitch(au, pitch)
  390. struct audio_channel *au;
  391. int pitch;
  392.    {
  393.       /* save current pitch in case we want to change
  394.        * the step table on the run
  395.        */
  396.    au->pitch = pitch;
  397.    au->step = step_table[pitch];
  398.    }
  399.  
  400. /* changing the current volume. You HAVE to get through there so that it 
  401.  * will work on EVERY machine.
  402.  */
  403. void set_play_volume(au, volume)
  404. struct audio_channel *au;
  405. int volume;
  406.    {
  407.    au->volume = volume;
  408.     au->scaled_volume = au->samp->volume_lookup[volume];
  409.    }
  410.  
  411. void set_play_position(au, pos)
  412. struct audio_channel *au;
  413. int pos;
  414.    {
  415.    au->pointer = int_to_fix(pos);
  416.             /* setting position too far must have this behavior for protracker */
  417.     if (au->pointer >= au->samp->fix_length)
  418.         au->mode = DO_NOTHING;
  419.    }
  420.  
  421.